package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.ForumService;
import tools.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2019/1/4.
 */
@Controller
@RequestMapping("/forum")
public class ForumController {

    @Resource
    ForumService forumService;

    @RequestMapping("/list")
    public String getBookList(Integer id,String title,@RequestParam(value="pageIndex",required =false) String pageIndex , HttpServletRequest request, Model model) {
        pageIndex = request.getParameter("pageIndex");
        Page pageObj = null;
        try {
            int currPageNo = 1;
            if (pageIndex == "" || pageIndex == null) {
                currPageNo = 1;
            } else {
                currPageNo = Integer.parseInt(pageIndex);
            }
            pageObj = new Page();
            pageObj.setCurrPageNo(currPageNo);
            pageObj.setPageSize(2);
            pageObj = forumService.ForumList(id,title, pageObj);
            model.addAttribute("pageObj",pageObj);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "index";
    }

   /* @RequestMapping("/getReplay")
    @ResponseBody
    public Page getReplyList( @RequestParam(value="pageIndex",required =false) String pageIndex ,HttpServletRequest request, @RequestParam(value="id",required=false)Integer id,@RequestParam(value="title",required=false)String title, Model model){
        //String pageIndex=request.getParameter("pageIndex");
        Page pageObj=null;
        try {
            int currPageNo = 1;
            if (pageIndex == "" || pageIndex == null) {
                currPageNo = 1;
            } else {
                currPageNo = Integer.parseInt(pageIndex);
            }
            pageObj = new Page();
            pageObj.setCurrPageNo(currPageNo);
            pageObj.setPageSize(2);
            pageObj = forumService.ForumList(a,id);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageObj;

    }*/

    @RequestMapping("/addReply")
    public String add(){
        return "replayInfo";
    }


    @RequestMapping("/deleteInfo")
    public String deleteInfo(Model model,HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        model.addAttribute("book",forumService.deleteByInvitationId(id));
        return "redirect:/forum/list";
    }



}
